package com.bert.dao;

import java.io.Serializable;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class GenericDaoImpl<T> implements GenericDao<T>{

	@Autowired
	SessionFactory sessionFactory;

	public void persist(T entity) {
		sessionFactory.getCurrentSession().persist(entity);
	}

	public void persist(List<T> entities) {
		for(T entity : entities){
			persist(entity);
		}
	}

	@SuppressWarnings("unchecked")
	public List<T> findAll(Class<T> entityClass) {
		return sessionFactory.getCurrentSession().createCriteria(entityClass).list();
	}

	public void delete(T entity) {
		sessionFactory.getCurrentSession().delete(entity);
	}

	@SuppressWarnings("unchecked")
	public T get(Class<T> entityClass, Serializable id) {
		return (T) sessionFactory.getCurrentSession().get(entityClass, id);
	}



}
